package cn.sh.ideal.web.system.management.user.management.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import cn.sh.ideal.web.system.management.user.management.entity.DeleteEntity;
import cn.sh.ideal.web.system.management.user.management.entity.InsertEntity;
import cn.sh.ideal.web.system.management.user.management.entity.SearchEntity;
import cn.sh.ideal.web.system.management.user.management.entity.UpdateEntity;

@Controller
@RequestMapping(value = "/data")
@PropertySource(value = {"classpath:jdbc.properties"})
public class UserManagementDataController {

	private RestTemplate rs = new RestTemplate();
	
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Value("${server_address}")
	private String serverAddress;
	/**
	 * 查询操作
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public String getUserInfo(@RequestBody SearchEntity paramter) {
		String userNo = paramter.getUserNo();
		String userName = paramter.getUserName();
		String belongToReglon = paramter.getBelongToReglon();
		String belongToDepartment = paramter.getBelongToDepartment();
		String AccountValidityPeriod = paramter.getAccountValidityPeriod();
		String gender = paramter.getGender();
		String emai = paramter.getEmai();
		String telephone = paramter.getTelephone();
		
		int page = paramter.getPage();
		int size = paramter.getSize();
		String url = serverAddress+"/selectUserInfo?userNo=" + userNo
				+ "&userName=" + userName + "&belongToReglon=" + belongToReglon
				+ "&belongToDepartment=" + belongToDepartment
				+ "&AccountValidityPeriod=" + AccountValidityPeriod
				+"&page=" + page
				+"&pageSize=" + size+"&gender=" + gender+"&emai=" + emai+"&telephone=" + telephone;

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);

		String result = exchange.getBody();
		return result;
	}

	/**
	 * 插入操作
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/insertUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public Object insertUserInfo(@RequestBody InsertEntity paramter) {

		String userNo = paramter.getUserNo();
		String userName = paramter.getUserName();
		String gender = paramter.getGender();
		String belongToReglon = paramter.getBelongToReglon();
		String belongToDepartment = paramter.getBelongToDepartment();
		String emai = paramter.getEmai();
		String telephone = paramter.getTelephone();
		String AccountValidityPeriod = paramter.getAccountValidityPeriod();
		String adminId = paramter.getAdminId();
		String userPasswd = paramter.getUserPasswd();
		String roleId = paramter.getRoleId();

		String url = serverAddress+"/insertUserInfo?userNo=" + userNo
				+ "&userName=" + userName + "&belongToReglon=" + belongToReglon
				+ "&belongToDepartment=" + belongToDepartment
				+ "&AccountValidityPeriod=" + AccountValidityPeriod
				+ "&gender=" + gender + "&emai=" + emai + "&telephone="
				+ telephone + "&userPasswd=" + userPasswd + "&adminId=" + adminId+ "&roleId=" + roleId;
		// 端口调用
		String result = rs.getForObject(url, String.class);

		return result;
	}

	/**
	 * 删除操作
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/deleteUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteUserInfo(@RequestBody DeleteEntity paramter) {
		String result ="";
		if (paramter.getUserNo().contains(",")) {
			// 微服务接口调用
			String url = serverAddress+"/deleteBatch?del_ids=";
			result = rs.getForObject(url+paramter.getUserNo(), String.class);
		} else {
			// 微服务接口调用
			String url1 = serverAddress+"/deleteByIdsAndUserNo?del_ids=";
			result = rs.getForObject(url1+paramter.getUserNo(), String.class);
		}
		return result;
	}

	/**
	 * 更新操作
	 * 
	 * @param paramter
	 * @return
	 */
	@RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateUserInfo(@RequestBody UpdateEntity paramter) {
		String id = paramter.getId();
		String userNo = paramter.getUserNo();
		String userName = paramter.getUserName();
		String gender = paramter.getGender();
		String belongToReglon = paramter.getBelongToReglon();
		String belongToDepartment = paramter.getBelongToDepartment();
		String emai = paramter.getEmai();
		String telephone = paramter.getTelephone();
		String userPasswd = paramter.getUserPasswd();
		String AccountValidityPeriod = paramter.getAccountValidityPeriod();
		String roleId = paramter.getRoleId();

		String url = serverAddress+"/updateUserInfo?userNo=" + userNo
				+ "&userName=" + userName + "&belongToReglon=" + belongToReglon
				+ "&belongToDepartment=" + belongToDepartment
				+ "&AccountValidityPeriod=" + AccountValidityPeriod
				+ "&gender=" + gender + "&emai=" + emai + "&telephone="
				+ telephone + "&Id=" + id + "&userPasswd=" + userPasswd + "&roleId=" + roleId;
		// 端口调用
		String result = rs.getForObject(url, String.class);

		return result;
	}
	
	/**
	 * 查询所有角色操作
	 * 
	 * @param 
	 * @return result
	 * @create zhangyy 2018.05.23
	 */
	@RequestMapping(value = "/getRoleList", method = RequestMethod.POST)
	@ResponseBody
	public String getRoleList(){

		String url = serverAddress+"/getRoleList" ;
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);

		String result = exchange.getBody();
		return result;
	}
}
